package com.woniuxy.lastfactory;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author 罗蔚
 * @Date 2022/3/23 15:12
 * @Description 一个工厂, 多个对象
 */
public class BeanFactory2 {
    Map allObject = new HashMap();
    Properties properties = new Properties();


    public Object getBean(String beanName) {
        try {
            properties.load(BeanFactory2.class.getClassLoader().getResourceAsStream("spring.properties"));
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                String value = properties.getProperty(key);
                //创建对象实例
                Object o = Class.forName(value).newInstance();
                //存到map中
                allObject.put(key, o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allObject.get(beanName);
    }
}
